<template>
  <div class="clusters">
    <div class="page-header">
      <h2>集群管理</h2>
      <el-button type="primary" @click="showCreateDialog">
        <el-icon><Plus /></el-icon>创建集群
      </el-button>
    </div>

    <el-card>
      <el-table
        v-loading="loading"
        :data="clusters"
        style="width: 100%"
      >
        <el-table-column prop="name" label="集群名称" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="created_by" label="创建者" />
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button-group>
              <el-button size="small" @click="editCluster(row)">编辑</el-button>
              <el-button size="small" type="danger" @click="deleteCluster(row)">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 创建/编辑集群对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑集群' : '创建集群'"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="集群名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useCloudStore } from '../stores/cloud'

const store = useCloudStore()
const loading = ref(false)
const dialogVisible = ref(false)
const isEdit = ref(false)
const formRef = ref(null)

const form = ref({
  name: '',
  description: ''
})

const rules = {
  name: [
    { required: true, message: '请输入集群名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  description: [
    { max: 200, message: '长度不能超过 200 个字符', trigger: 'blur' }
  ]
}

const clusters = ref([])

onMounted(async () => {
  await fetchClusters()
})

async function fetchClusters() {
  loading.value = true
  try {
    await store.fetchClusters()
    clusters.value = store.clusters
  } catch (error) {
    ElMessage.error('获取集群列表失败')
  } finally {
    loading.value = false
  }
}

function showCreateDialog() {
  isEdit.value = false
  form.value = {
    name: '',
    description: ''
  }
  dialogVisible.value = true
}

function editCluster(cluster) {
  isEdit.value = true
  form.value = { ...cluster }
  dialogVisible.value = true
}

async function deleteCluster(cluster) {
  try {
    await ElMessageBox.confirm(
      `确定要删除集群 "${cluster.name}" 吗？`,
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    // TODO: 实现删除集群的逻辑
    ElMessage.success('删除成功')
    await fetchClusters()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

async function submitForm() {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    // TODO: 实现创建/更新集群的逻辑
    ElMessage.success(isEdit.value ? '更新成功' : '创建成功')
    dialogVisible.value = false
    await fetchClusters()
  } catch (error) {
    ElMessage.error('表单验证失败')
  }
}
</script>

<style scoped>
.clusters {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
}
</style> 